package com.hqd.ch03.v6.reader.xml.doc.support;

import com.hqd.ch03.v6.io.Resource;
import com.hqd.ch03.v6.reader.xml.doc.DocumentLoader;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class DefaultDocumentLoader implements DocumentLoader {
    @Override
    public Document loadDocument(Resource resource) {
        try {
            InputSource inputSource = new InputSource(resource.getInputStream());
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            return docBuilder.parse(inputSource);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
